package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CatagoryController {

    @Autowired
    private CategoryService categoryService;


    //新增分类
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("categoryDTO={}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    //分类分页查询
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryqueryDTO){
        PageResult pageResult = categoryService.pageQuery(categoryqueryDTO);
        return Result.success(pageResult);
    }

    //根据id删除分类
    @DeleteMapping
    public Result deleteCategory(String id){
        log.info("要删除的分类id={}", id);
        categoryService.deleteCategory(id);
        return Result.success();
    }

    //修改分类
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    //分类启用禁用
    @PostMapping("/status/{status}")
    public Result StartOrStop(@PathVariable("status") Integer status,Long id){
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    //根据类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> getCategoryByType(Integer type){
        List<Category> list = categoryService.getCategoryByType(type);
        return Result.success(list);
    }
}
